home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / info-service / www / src / midaswww-1.0 / SGMLPlainText.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-11-16  |  9.7 KB  |  279 lines

  1. /*==================================================================*/
  2. /*                                                                  */
  3. /* SGMLPlainTextObject                                              */
  4. /*                                                                  */
  5. /* T.Johnson - (TonyJ@Slacvx.Slac.Stanford.Edu)           June.92   */
  6. /*                                                                  */
  7. /* Defines a plain text segment for the SGMLHyper widget            */
  8. /*                                                                  */
  9. /*==================================================================*/
  10.  
  11. #include <X11/IntrinsicP.h>
  12. #include <X11/StringDefs.h>
  13. #include <X11/CoreP.h>
  14. #include "SGMLPlainTextP.h"
  15.  
  16. /* 
  17.   Private functions 
  18. */
  19.  
  20. static int LineSize();
  21.  
  22. /*
  23.   Widget class methods
  24. */
  25.  
  26. static void    Initialize();
  27. static void    Destroy();
  28. static void    ComputeSize();
  29. static void    Redisplay();
  30.  
  31. #define Offset(field) XtOffsetOf(SGMLPlainTextRec,sgml_plain_text.field)
  32.  
  33. static XtResource resources[] = {
  34.  
  35.     {SGMLNtabs,SGMLCTabs,XtRInt,sizeof(int),
  36.     Offset (tabs),XtRImmediate,(XtPointer)0},
  37.  
  38. };
  39. #undef Offset
  40.  
  41. /*---------------------------------------------------------------*/
  42. /* Static initialisation of the class record                     */
  43. /*---------------------------------------------------------------*/
  44.  
  45. SGMLPlainTextClassRec  sGMLPlainTextClassRec = {
  46.     {
  47.     (WidgetClass) &sGMLTextClassRec,     /* superclass            */
  48.     "SGMLPlainText",                     /* class_name            */
  49.     sizeof(SGMLPlainTextRec),            /* widget_size           */
  50.     NULL,                                /* class_initialize      */
  51.     NULL,                                /* class_part_initialize */
  52.     FALSE,                               /* class_inited          */
  53.     Initialize,                          /* initialize            */
  54.     NULL,                                /* initialize_hook       */
  55.     NULL,                                /* obj1                  */
  56.     NULL,                                /* obj2                  */
  57.     0,                                   /* obj3                  */
  58.     resources,                           /* resources             */
  59.     XtNumber(resources),                 /* num_resources         */
  60.     NULLQUARK,                           /* xrm_class             */
  61.     0,                                   /* obj4                  */
  62.     0,                                   /* obj5                  */
  63.     0,                                   /* obj6                  */
  64.     0,                                   /* obj7                  */
  65.     Destroy,                             /* destroy               */
  66.     NULL,                                /* obj8                  */
  67.     NULL,                                /* obj9                  */
  68.     NULL,                                /* set_values            */
  69.     NULL,                                /* set_values_hook       */
  70.     NULL,                                /* obj10                 */
  71.     NULL,                                /* get_values_hook       */
  72.     NULL,                                /* obj11                 */
  73.     XtVersion,                           /* version               */
  74.     NULL,                                /* callback private      */
  75.     NULL,                                /* obj12                 */
  76.     NULL,                                /* obj13                 */
  77.     NULL,                                /* obj14                 */
  78.     NULL,                                /* extension             */
  79.     },
  80.     {
  81.     ComputeSize,                   /* compute_size          */
  82.     SGMLInheritAdjustSize,               /* adjust_size           */
  83.     SGMLInheritAdjustPosition,        /* adjust_position       */
  84.     Redisplay,                           /* expose                */
  85.     SGMLInheritActivate,                 /* activate              */
  86.     SGMLInheritHilite,                   /* hilite                */
  87.     SGMLInheritContains,                 /* contains              */
  88.     SGMLInheritCallCreateCallback,       /* call_create_callback  */
  89.     SGMLInheritCallMapCallback,          /* call_map_callback     */
  90.     SGMLInheritMakeVisible,              /* make_visible          */
  91.     NULL,                                /* extension             */
  92.     },
  93.     {
  94.     NULL,                                /* ignore                */
  95.     }
  96. };
  97.  
  98.  
  99. WidgetClass sGMLPlainTextObjectClass = (WidgetClass) &sGMLPlainTextClassRec;
  100.  
  101. /*--------------------------------------------------------------*/
  102. /* Initialize:                                                  */
  103. /*--------------------------------------------------------------*/
  104.  
  105. static void Initialize (request, new)
  106. SGMLTextObject request, new;
  107. {
  108. }
  109.  
  110. /*--------------------------------------------------------------*/
  111. /* Destroy the widget: release all memory alocated              */
  112. /*--------------------------------------------------------------*/
  113.  
  114. static void Destroy (w)
  115. SGMLTextObject w;
  116. {
  117. }
  118.  
  119. /*--------------------------------------------------------------*/
  120. /* Redisplay                                                    */
  121. /*--------------------------------------------------------------*/
  122.  
  123. static void Redisplay(t,event,region)
  124. SGMLPlainTextObject t;
  125. XEvent *event;
  126. Region region;
  127. {
  128.    Position y = t->sgml_text.begin.y;
  129.    Position margin = t->sgml_text.margin;
  130.    Dimension width  = t->sgml_text.width;
  131.    Dimension height = t->sgml_text.height;
  132.  
  133.    if(XRectInRegion(region,margin,y,width,height) != RectangleOut)
  134.    {
  135.       Display *dpy = XtDisplayOfObject((Widget) t);
  136.       Window  wind = XtWindowOfObject((Widget) t);
  137.       GC gc =  t->sgml_text.gc;
  138.       int tabs = t->sgml_plain_text.tabs;
  139.  
  140.       Position x = t->sgml_text.begin.x;
  141.       Dimension depth = t->sgml_text.depth;
  142.       Dimension ascent = t->sgml_text.ascent;
  143.       char *p = t->sgml_text.text;
  144.       char *line = p;
  145.  
  146.       for (;; p++)
  147.         {
  148.           if (*p == '\n' || *p == '\0') 
  149.             { 
  150.                if (p != line && XRectInRegion(region,x,y,width,depth) != RectangleOut)
  151.                  if (tabs)
  152.                    {
  153.                      int nt = 0, pos=0;
  154.                      char *q, *copy;
  155.  
  156.                      for (q = line; q < p; q++) if (*q == '\t') nt++;
  157.         
  158.                      copy = XtMalloc(p-line + nt * tabs);
  159.                      
  160.                      for (q = line; q < p; q++)
  161.                        if (*q != '\t') copy[pos++] = *q;
  162.                        else for ( copy[pos++] = ' '; pos%tabs ; ) copy[pos++] = ' ';
  163.                          
  164.                      XDrawString(dpy, wind,gc, x, y+ascent, copy, pos);
  165.                      XtFree(copy); 
  166.                    } 
  167.                  else XDrawString(dpy, wind,gc, x, y+ascent, line, p-line); 
  168.  
  169.                if (*p == '\0') break;
  170.                
  171.                x = margin;  
  172.                y += depth;
  173.                line = p+1;
  174.             }
  175.         }
  176.    }
  177. }
  178.  
  179. /*--------------------------------------------------------------*/
  180. /* Compute Size:                                                */
  181. /*--------------------------------------------------------------*/
  182.  
  183. static void ComputeSize(w,geom)
  184. SGMLTextObject w;
  185. SGMLGeometry *geom;
  186. {
  187.    Dimension left_clearance  = w->sgml_text.left_margin;
  188.    int nlines;
  189.  
  190.    /*
  191.     * Break before?
  192.     */  
  193.  
  194.    if ( w->sgml_text.break_before )
  195.    {
  196.      _SGMLBreak(geom,w->sgml_text.space_before);
  197.    } 
  198.  
  199.    if (w->sgml_text.depth  > geom->coord.depth) geom->coord.depth =  w->sgml_text.depth;
  200.    if (left_clearance > geom->coord.x    ) geom->coord.x = left_clearance;
  201.  
  202.    w->sgml_text.begin  = geom->coord;
  203.  
  204.    nlines = LineSize(w,&geom->coord.x,&geom->actual_width);
  205.  
  206.    w->sgml_text.height = w->sgml_text.depth  * nlines;   
  207.    w->sgml_text.margin = left_clearance;
  208.    w->sgml_text.width = geom->actual_width - left_clearance;
  209.    
  210.    geom->coord.y += w->sgml_text.height -  w->sgml_text.depth;
  211.    geom->coord.depth  = w->sgml_text.depth;
  212.    geom->space = 0;
  213.    geom->broken = FALSE;
  214.  
  215.    w->sgml_text.end = geom->coord;
  216.  
  217.    /*
  218.     * Break after?
  219.     */  
  220.  
  221.    if ( w->sgml_text.break_after )
  222.    {
  223.      _SGMLBreak(geom,w->sgml_text.space_after);
  224.    } 
  225. }
  226.  
  227. /*-----------------------------------------------------------------------*/
  228. /* Private routines                                                      */
  229. /*-----------------------------------------------------------------------*/
  230. /* LineSize: In the plain text case, just count the newlines             */
  231. /*-----------------------------------------------------------------------*/
  232.  
  233. static int LineSize(w, inoutx, inoutwidth)
  234. SGMLPlainTextObject w;
  235. Position *inoutx;
  236. Dimension *inoutwidth; 
  237. {
  238.   Position x = *inoutx;
  239.   Dimension width, maxWidth = *inoutwidth;
  240.   Dimension left_clearance  = w->sgml_text.left_margin;
  241.   int nlines = 1;
  242.   int tabs = w->sgml_plain_text.tabs;
  243.   char *p = w->sgml_text.text;
  244.   int size = 0;
  245.  
  246.   for (; *p != '\0'; p++)
  247.     {
  248.        if (*p == '\n') 
  249.          {
  250.            width = size * w->sgml_text.spacing;
  251.            if (x+width > maxWidth) maxWidth = x+width;
  252.            nlines++;  
  253.            size = 0;
  254.            x = left_clearance; 
  255.          }
  256.        else if (*p == '\t' && tabs)  size += tabs - size%tabs; 
  257.        else size++;
  258.     }
  259.  width = size * w->sgml_text.spacing;
  260.  if (x+width > maxWidth) maxWidth = x+width;
  261.  x += width; 
  262.  
  263.  *inoutx = x; 
  264.  *inoutwidth = maxWidth;   
  265.  return nlines;
  266. }
  267. /*-----------------------------------------------------------------------*/
  268. /* Create a new SGMLPlainTextObject                                      */
  269. /*-----------------------------------------------------------------------*/
  270.  
  271. Widget SGMLCreatePlainText(parent,name,al,ac)
  272. Widget parent;
  273. char   *name;
  274. ArgList al;
  275. int     ac;
  276. {
  277.     return XtCreateWidget(name,sGMLPlainTextObjectClass,parent,al,ac);
  278. }
  279.